var MyPromise = /** @class */ (function () {
    function MyPromise(executor) {
        var _this = this;
        this.status = 'pending';
        this.data = null;
        this.callbackList = [];
        /**
         * resolve函数
         *
         * 如果status不为pending则不触发，会将status改成fulfilled
         * 循环调用successCb的时候应该放在setTimeout里
         * @param data 成功时返回的数据
         */
        var resolve = function (data) {
            if (_this.status === 'pending') {
                _this.status = 'fulfilled';
                _this.data = data;
                setTimeout(function () {
                    for (var _i = 0, _a = _this.callbackList; _i < _a.length; _i++) {
                        var _b = _a[_i], successCb = _b[0], failCb = _b[1];
                        successCb(_this.data);
                    }
                });
            }
        };
        /**
         * reject函数
         *
         * 如果status不为pending则不触发，会将status改成rejected
         * @param reason 失败时返回的数据
         */
        var reject = function (reason) {
            if (_this.status === 'pending') {
                _this.status = 'rejected';
                _this.data = reason;
                setTimeout(function () {
                    for (var _i = 0, _a = _this.callbackList; _i < _a.length; _i++) {
                        var _b = _a[_i], successCb = _b[0], failCb = _b[1];
                        failCb(_this.data);
                    }
                });
            }
        };
        try {
            executor(resolve, reject);
        }
        catch (e) {
            reject(e);
        }
    }
    MyPromise.resolve = function (value) {
        return new MyPromise(function (resolve, reject) {
            if (value instanceof MyPromise) {
                value.then(resolve, reject);
            }
            else {
                resolve(value);
            }
        });
    };
    MyPromise.reject = function (reason) {
        return new MyPromise(function (resolve, reject) {
            reject(reason);
        });
    };
    MyPromise.all = function (promiseList) {
        return new MyPromise(function (resolve, reject) {
            var results = [];
            var resultCount = 0;
            var _loop_1 = function (i) {
                var item = promiseList[i];
                MyPromise.resolve(item).then(function (data) {
                    results[i] = data;
                    resultCount++;
                    if (resultCount === promiseList.length) {
                        resolve(results);
                    }
                }, reject);
            };
            for (var i = 0; i < promiseList.length; i++) {
                _loop_1(i);
            }
        });
    };
    MyPromise.race = function (promiseList) {
        return new MyPromise(function (resolve, reject) {
            for (var i = 0; i < promiseList.length; i++) {
                var item = promiseList[i];
                MyPromise.resolve(item).then(resolve, reject);
            }
        });
    };
    /**
     * then函数
     *
     * 如果已经fulfilled或rejected，则需要把task放入setTimeout
     * @param successCb 成功时回调函数，返回值为普通值或者Promise，也可以直接传递一个值，
     * @param failCb 失败回调，返回值为普通值或者Promise，也可以直接传递一个值
     */
    MyPromise.prototype.then = function (successCb, failCb) {
        var _this = this;
        successCb = typeof successCb === 'function' ? successCb : function (v) { return v; };
        failCb =
            typeof failCb === 'function'
                ? failCb
                : function (v) {
                    throw v;
                };
        return new MyPromise(function (resolve, reject) {
            var task = function (cb) {
                try {
                    var newData = cb(_this.data);
                    if (newData instanceof MyPromise) {
                        newData.then(resolve, reject);
                    }
                    else {
                        resolve(newData);
                    }
                }
                catch (e) {
                    reject(e);
                }
            };
            if (_this.status === 'fulfilled') {
                setTimeout(function () {
                    task(successCb);
                });
            }
            else if (_this.status === 'rejected') {
                setTimeout(function () {
                    task(failCb);
                });
            }
            else {
                _this.callbackList.push([function () { return task(successCb); }, function () { return task(failCb); }]);
            }
        });
    };
    /**
     * catch函数
     *
     * @param failCb 失败回调，返回值为普通值或者Promise，也可以直接传递一个值
     */
    MyPromise.prototype["catch"] = function (failCb) {
        return this.then(undefined, failCb);
    };
    return MyPromise;
}());
console.log('start');
new MyPromise(function (resolve, reject) {
    // setTimeout(() => {
    //   resolve('123');
    // }, 1000);
    resolve('123');
})
    .then(function (data) {
    console.log(data);
    throw '1';
    // return new MyPromise((resolve) => {
    //   resolve('456');
    // });
})
    .then(function (data) {
    console.log(data);
})["catch"](function (e) { return console.log(e); });
console.log('end');
